package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.Area;
import com.hpjpw.ido.domain.Enterprise;
import com.hpjpw.ido.service.IAreaService;
import com.hpjpw.ido.service.IEnterpriseService;
import com.hpjpw.idoapp.domain.EnterpriseImportVo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseImportService {
    private static final Logger log = LoggerFactory.getLogger(EnterpriseImportService.class);
    @Autowired
    IEnterpriseService enterpriseService;
    @Autowired
    IAreaService areaService;

    /* 创建一条企业数据 */
    public boolean createEnterprise(EnterpriseImportVo enterpriseImport){
        if (StringUtils.isNull(enterpriseImport)){
            return false;
        }
        /* 由于Enterprise实体中id和imgUrl没有具体赋值，所以不能有参构造函数创建 */
        Enterprise enterprise = new Enterprise();
        enterprise.setName(enterpriseImport.getName());
        enterprise.setCode(enterpriseImport.getCode());
        enterprise.setAddress(enterpriseImport.getAddress());
        enterprise.setEmail(enterpriseImport.getEmail());
        /* 匹配地区，同样查询列表，只要列表不为空即查询成功，取第一项地区id */
        Area area = new Area();
        area.setName(enterpriseImport.getAreaName());
        List<Area> areas = areaService.selectAreaList(area);
        if (StringUtils.isNotNull(areas)){
            enterprise.setAreaId(areas.get(0).getId());
        }else {
            return false;
        }
        enterprise.setPhone(enterpriseImport.getPhone());
        enterprise.setOrderNum(enterpriseImport.getOrderNum());
        enterpriseService.insertEnterprise(enterprise);
        return true;
    }

    /* 导入企业 */
    public String importEnterprise(List<EnterpriseImportVo> enterpriseImportVoList){
        /* 判断导入总表是否为空 */
        if (StringUtils.isNull(enterpriseImportVoList)||enterpriseImportVoList.size() == 0){
            throw new CustomException("导入企业数据不能为空！");
        }
        /* 表格中成功导入与失败导入条数 */
        int successNum = 0;
        int failureNum = 0;
        /* 成功导入以及失败导入的消息提示 */
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        /* 对每一条企业数据进行校验 */
        for (EnterpriseImportVo item : enterpriseImportVoList) {
            /* 校验企业注册码是否已存在，存在则抛出异常 */
            Enterprise enterprise = new Enterprise();
            enterprise.setCode(item.getCode());
            List<Enterprise> enterprises = enterpriseService.selectEnterpriseList(enterprise);
            /* 按照注册码搜索的企业列表为空，说明注册码无重复，可导入 */
            if (StringUtils.isNull(enterprises)||enterprises.size() == 0){
                /* 创建企业数据成功，即成功导入 */
                if (createEnterprise(item)){
                    successNum++;
                    String msg = "<br/>" + successNum + "：注册码 " + item.getCode() + " 导入成功！";
                    successMsg.append(msg);
                }else {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "：地区 " + item.getAreaName() + " 不存在！";
                    failureMsg.append(msg);
                }

            }else {
                failureNum++;
                String msg = "<br/>" + failureNum + "：注册码 " + item.getCode() + " 导入失败！";
                failureMsg.append(msg);
            }
        }

        return successMsg.toString();
    }
}
